/*
 * Copyright (c) 2020 IUV Inc. All rights reserved.
 * 项目名称：IUV TXY
 * 文件名：FriendController.java
 * Date：20-10-23 下午3:45
 * Author：Will
 */

package com.iuv.imdemo.controller;

import com.iuv.common.CommonOperateCode;
import com.iuv.imdemo.dto.FrindPageBean;
import com.iuv.imdemo.service.ImFriendService;
import com.iuv.imdemo.util.RespondUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * FriendController
 *
 * @author Will
 * @date 15:45
 */
@RequestMapping("friend")
@Controller
public class FriendController {
    @Autowired
    private ImFriendService imFriendService;

    /**
     * 添加好友
     * @param friendUserId
     * @param remark
     * @param request
     * @return
     */
    @RequestMapping("addFriend")
    @ResponseBody
    public Map<String, Object> addFriend(@RequestParam String friendUserId,@RequestParam String remark, HttpServletRequest request) {
        friendUserId = StringUtils.trimToNull(friendUserId);
        String userId = (String) request.getSession().getAttribute(UserController.SESSION_USER_ID);

        /**
         * TODO 问题五 调试代码
         * 此处有一段代码逻辑错误，导致用户添加成功，前端页面缺显示添加失败
         */
        if (friendUserId == null || userId == null|| imFriendService.addFirend(userId,friendUserId,remark)) {
            return RespondUtil.respondFail(CommonOperateCode.SysCode.ADD_FRIEND_FAIL, CommonOperateCode.SysMsg.ADD_FRIEND_FAIL);
        }
        /**
         * END
         */
        return RespondUtil.respondSuccess(CommonOperateCode.SysCode.ADD_FRIEND_SUCCESS, CommonOperateCode.SysMsg.ADD_FRIEND_SUCCESS);
    }

    /**
     * 修改好友备注
     * @param friendUserId
     * @param remark
     * @param request
     * @return
     */
    @RequestMapping("updateFirendRemark")
    @ResponseBody
    public Map<String, Object> updateFirendRemark(@RequestParam String friendUserId,@RequestParam String remark, HttpServletRequest request) {
        friendUserId = StringUtils.trimToNull(friendUserId);
        String userId = (String) request.getSession().getAttribute(UserController.SESSION_USER_ID);
        if (friendUserId == null || userId == null) {
            return RespondUtil.respondFail(CommonOperateCode.SysCode.UPDATE_FRIEND_FAIL, CommonOperateCode.SysMsg.UPDATE_FRIEND_FAIL);
        }
        imFriendService.updateFirendRemark(userId,friendUserId,remark);
        return RespondUtil.respondSuccess(CommonOperateCode.SysCode.UPDATE_FRIEND_SUCCESS, CommonOperateCode.SysMsg.UPDATE_FRIEND_SUCCESS);
    }

    /**
     * 删除好友
     * @param friendUserId
     * @param request
     * @return
     */
    @RequestMapping("deleteFirend")
    @ResponseBody
    public Map<String, Object> deleteFirend(@RequestParam String friendUserId, HttpServletRequest request) {
        friendUserId = StringUtils.trimToNull(friendUserId);
        String userId = (String) request.getSession().getAttribute(UserController.SESSION_USER_ID);
        if (friendUserId == null || userId == null) {
            return RespondUtil.respondFail(CommonOperateCode.SysCode.DELETE_FRIEND_FAIL, CommonOperateCode.SysMsg.DELETE_FRIEND_FAIL);
        }
        imFriendService.deleteFirend(userId,friendUserId);
        return RespondUtil.respondSuccess(CommonOperateCode.SysCode.DELETE_FRIEND_SUCCESS, CommonOperateCode.SysMsg.DELETE_FRIEND_SUCCESS);
    }

    /**
     * 删除所有好友
     * @param request
     * @return
     */
    @RequestMapping("deleteAllFirend")
    @ResponseBody
    public Map<String, Object> deleteAllFirend(HttpServletRequest request) {
        String userId = (String) request.getSession().getAttribute(UserController.SESSION_USER_ID);
        if ( userId == null) {
            return RespondUtil.respondFail(CommonOperateCode.SysCode.DELETE_FRIEND_FAIL, CommonOperateCode.SysMsg.DELETE_FRIEND_FAIL);
        }
        imFriendService.deleteAllFirend(userId);
        return RespondUtil.respondSuccess(CommonOperateCode.SysCode.DELETE_FRIEND_SUCCESS, CommonOperateCode.SysMsg.DELETE_FRIEND_SUCCESS);
    }

    /**
     * 校验好友
     * @param friendUserId
     * @param request
     * @return
     */
    @RequestMapping("checkFirend")
    @ResponseBody
    public Map<String, Object> checkFirend(@RequestParam String friendUserId, HttpServletRequest request) {
        friendUserId = StringUtils.trimToNull(friendUserId);
        String userId = (String) request.getSession().getAttribute(UserController.SESSION_USER_ID);
        if (friendUserId == null || userId == null) {
            return RespondUtil.respondFail(CommonOperateCode.SysCode.CHECK_FRIEND_FAIL, CommonOperateCode.SysMsg.CHECK_FRIEND_FAIL);
        }
        imFriendService.checkFirend(userId,friendUserId);
        return RespondUtil.respondSuccess(CommonOperateCode.SysCode.CHECK_FRIEND_SUCCESS, CommonOperateCode.SysMsg.CHECK_FRIEND_SUCCESS);
    }

    /**
     * 分页查询好友列表
     * @param startIndex
     * @param request
     * @return
     */
    @RequestMapping("queryFriendByPage")
    @ResponseBody
    public FrindPageBean queryFriendByPage(@RequestParam Integer startIndex, HttpServletRequest request) {
        String userId = (String) request.getSession().getAttribute(UserController.SESSION_USER_ID);
        if ( userId == null) {
            return null;
        }
        if(startIndex==null)
        {
            startIndex=0;
        }
       return  imFriendService.queryFriendByPage(userId,startIndex);
    }
}
